package com.easy;

import java.util.*;

/**
 * Created by Lxk on 2019/6/3.
 */
public class Solution350 {

    public int[] intersect(int[] nums1, int[] nums2) {
        Map<Integer,Integer> baseMap = new HashMap<>();
        for(Integer val : nums1){
            if(baseMap.keySet().contains(val)){
                baseMap.put(val,baseMap.get(val)+1);
            }else{
                baseMap.put(val,1);
            }
        }
        List<Integer> resList = new ArrayList<>();
        for(Integer val : nums2){
            if(baseMap.keySet().contains(val)&&baseMap.get(val)>0){
                resList.add(val);
                baseMap.put(val,baseMap.get(val)-1);
            }
        }
        int[] res = new int[resList.size()];
        for(int i = 0;i < resList.size(); i++){
            res[i] = resList.get(i);
        }
        return res;
    }

}
